package com.bawei.cashier


import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.mvvm.BaseActivity
import com.bawei.cashier.databinding.ActivityCashierDetailBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/cashier/CashierDetailActivity")
class CashierDetailActivity : BaseActivity<ActivityCashierDetailBinding, CashierViewModel>() {
    @JvmField
    @Autowired
    var id = 1
    lateinit var cashierDetailAdapter: CashierDetailAdapter
    override fun initView() {
        ARouter.getInstance().inject(this)
        viewModel.payerDetailById(id)
        val payerdetailAllcheck = binding.payerdetailAllcheck
        cashierDetailAdapter = CashierDetailAdapter(viewModel.apiService, this)
        binding.payerdetailRec.adapter = cashierDetailAdapter
        binding.payerdetailRec.layoutManager = LinearLayoutManager(this)
        binding.payerdetailAllcheck.setOnClickListener {
            CashierDetailAdapter.cashierDetailAdapter2.data.forEach {
                it.isCheck = payerdetailAllcheck.isChecked
                allMoney()
            }
            CashierDetailAdapter.cashierDetailAdapter2.notifyDataSetChanged()
        }
        CashierDetailAdapter.cashierDetailAdapter2.addChildClickViewIds(R.id.payerdetail2_check)
        CashierDetailAdapter.cashierDetailAdapter2.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.payerdetail2_check -> {
                    CashierDetailAdapter.cashierDetailAdapter2.data[position].isCheck =
                        !CashierDetailAdapter.cashierDetailAdapter2.data[position].isCheck
                    isCheck()
                    allMoney()
                    CashierDetailAdapter.cashierDetailAdapter2.notifyDataSetChanged()
                }
            }
        }
        binding.payerdetailSubmitPieceAll.setOnClickListener {
            ARouter.getInstance().build("/app/pay/PayActivity").navigation()
            binding.payerdetailAllcheck.isChecked=false
            binding.payerdetailAllpirce.text="金额：0.0"

        }

    }

    fun isCheck() {
        var list = mutableListOf<Boolean>()
        CashierDetailAdapter.cashierDetailAdapter2.data.forEach {
            list.add(it.isCheck)
        }
        binding.payerdetailAllcheck.isChecked = false !in list

    }

    fun allMoney() {
        var sum = 0
        CashierDetailAdapter.cashierDetailAdapter2.data.forEach {
            if (it.isCheck)
                sum += it.actualAmount
        }
        binding.payerdetailAllpirce.text = "合计" + sum.toString()
    }

    override fun initData() {
        viewModel.payerDetailSuccess.observe(this) {
            binding.payerdetailArea.text = it.projectName
            binding.payerdetailPirce.text = "账单金额" + it.totalAmount.toString()
            binding.payerdetailUsername.text = "户主" + it.payerName
            binding.payerdetailPhone.text = "联系电话" + it.contactNumber
            cashierDetailAdapter.data.clear()
            cashierDetailAdapter.data.add(it)
            cashierDetailAdapter.notifyDataSetChanged()
        }
    }
}